package com.csun.dsms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.mapper.HgUserMapper;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.huang.HgOaLoginData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.model.huang.HgUser;
import com.csun.dsms.service.HuangGangService;
import com.csun.dsms.utils.pt.HuangGangUtil;
import com.csun.dsms.utils.pt.OAUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class HuangGangServiceImpl extends BaseService<HgUser> implements HuangGangService {

    @Resource
    private HgUserMapper hgUserMapper;

    @Override
    public HgUser login(String oToken) {
        //{"code":2,"status":1000,"msg":"data����Ϊ��","data":null,"encrypt":false,"algorithm":0,"key":null,"reqId":null,"rsa":false}
        String aesRes = HuangGangUtil.login(oToken);
//                {"idCard":"420704199309281692",
//                "userId":"1834603394435493889",
//                "mobilePhone":"16602722600",
//                "name":"��־��",
//                "userType":"1",
//                "communityCode":"1327"}
        System.out.println(aesRes);
        JSONObject aesJsonBean = JSONObject.parseObject(aesRes);
        if (aesJsonBean!=null && aesJsonBean.getString("userId")!=null){
            String userId = aesJsonBean.getString("userId");
            String idCard = aesJsonBean.getString("idCard");
            String mobilePhone = aesJsonBean.getString("mobilePhone");
            String name = aesJsonBean.getString("name");

            System.out.println("getResultSuccess");
            System.out.println("userId: "+userId);
            System.out.println("idCard: "+idCard);
            System.out.println("mobilePhone: "+mobilePhone);
            System.out.println("name: "+name);
            HgQueryDto hgQueryDto = new HgQueryDto();
            hgQueryDto.setPhone(mobilePhone);
            HgUser hgUser = hgUserMapper.selectUserByPhone(hgQueryDto);
            if (hgUser!=null){
                hgUser.setHuangGangId(userId);
                hgUser.setLastLoginTime(new Date());
                hgUserMapper.updateByPrimaryKeySelective(hgUser);
                return hgUser;
            }
            return null;
        }
        return null;
    }

    @Override
    public String getPhone(String oToken) {
        String aesRes = HuangGangUtil.login(oToken);
        JSONObject aesJsonBean = JSONObject.parseObject(aesRes);
        if (aesJsonBean!=null && aesJsonBean.getString("userId")!=null){
            String mobilePhone = aesJsonBean.getString("mobilePhone");
            return mobilePhone;
        }
        return null;
    }

    @Override
    public HgUser oaLogin(String ticket) {
        String result = OAUtils.login(ticket);
        if (result == null){
            return null;
        }
        HgOaLoginData hgOaLoginData = JSONObject.parseObject(result, HgOaLoginData.class);
        HgOaLoginData.AuthenticationSuccess authenticationSuccess =
                hgOaLoginData.getServiceResponse().getAuthenticationSuccess();
        String mobile = authenticationSuccess.getAttributes().getMobile().get(0);
        String userId = authenticationSuccess.getAttributes().getUserId().get(0);
        HgQueryDto hgQueryDto = new HgQueryDto();
        hgQueryDto.setPhone(mobile);
        HgUser hgUser = hgUserMapper.selectUserByPhone(hgQueryDto);
        if (hgUser!=null){
            hgUser.setOaUserId(userId);
            hgUser.setLastLoginTime(new Date());
            hgUserMapper.updateByPrimaryKeySelective(hgUser);
            return hgUser;
        }
        return null;
    }



    @Override
    public HgUser oaLogin2(String ticket) {
        String result = OAUtils.login2(ticket);
        log.info("oaLogin _ "+result);
        return null;
    }

    @Override
    public List<HgUser> queryUserList(HgQueryDto hgQueryDto) {
        return hgUserMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public Integer syncOAUser() {
        List<HgUser> userList = hgUserMapper.selectAll();


        return null;
    }

    @Override
    public Integer syncHgUser() {
        return null;
    }
}
